package com.example.baguwen.repository;

import com.example.baguwen.entity.BaguwenFavorite;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface BaguwenFavoriteRepository extends JpaRepository<BaguwenFavorite, Long> {
    
    Optional<BaguwenFavorite> findByUserIdAndBaguwenId(Long userId, Long baguwenId);
    
    boolean existsByUserIdAndBaguwenId(Long userId, Long baguwenId);

    @Query(
            value = "SELECT f FROM BaguwenFavorite f JOIN FETCH f.baguwen b WHERE f.userId = :userId AND b.isDeleted = false",
            countQuery = "SELECT COUNT(f) FROM BaguwenFavorite f WHERE f.userId = :userId AND f.baguwen.isDeleted = false"
    )
    Page<BaguwenFavorite> findAllWithBaguwenByUserId(@Param("userId") Long userId, Pageable pageable);

} 